

# print("0,0,0,0,0,0,0")
# print("0,1,0,0,0,0,0")
# print("0,1,0,0,0,0,0")
# print("0,1,0,0,1,0,0")
# print("0,1,1,1,1,1,0")
# print("0,0,0,0,1,0,0")
# print("0,0,0,0,1,0,0")
# print("0,0,0,0,1,0,0")
# print("0,0,0,0,0,0,0")



# print("0,0,0,0,0,0,0")
# print("0,1,1,1,1,1,0")
# print("0,1,0,0,0,1,0")
# print("0,1,0,0,0,1,0")
# print("0,1,1,1,1,1,0")
# print("0,0,0,0,0,1,0")
# print("0,0,0,0,0,1,0")
# print("0,0,0,0,0,1,0")
# print("0,0,0,0,0,0,0")
#
# print()
# print("0,0,0,0,0,0,0")
# print("0,1,1,1,0,1,0")
# print("0,1,0,0,0,1,0")
# print("0,1,0,0,0,1,0")
# print("0,0,1,1,1,1,0")
# print("0,0,0,0,0,1,0")
# print("0,0,0,0,0,1,0")
# print("0,0,0,0,0,1,0")
# print("0,0,0,0,0,0,0")



# import time
#
# import numpy as np
# from matplotlib import pyplot as plt
#
# # Rastigrin函数
# def rastrigin_function(x1, x2):
#     return -(20 + x1**2 + x2**2 - 10 * (np.cos(2 * np.pi * x1) + np.cos(2 * np.pi * x2)))
#
# # 遗传算法
# def genetic_algorithm(population_size, generations, crossover_rate, mutation_rate, search_range, time_limit=None, fitness_limit=None, stall_generations=None, stall_time_limit=None):
#     population = np.random.uniform(low=search_range[0], high=search_range[1], size=(population_size, 2))
#
#     best_fitness_history = []
#     best_individual_history = []
#
#     start_time = time.time()
#     prev_best_fitness = None
#     stall_count = 0
#
#     for generation in range(generations):
#         fitness_values = np.array([rastrigin_function(x[0], x[1]) for x in population])
#
#         best_index = np.argmin(fitness_values)
#         best_fitness = fitness_values[best_index]
#         best_individual = population[best_index]
#
#         best_fitness_history.append(best_fitness)
#         best_individual_history.append(best_individual)
#
#         # 判断是否终止算法
#         if time_limit is not None and time.time() - start_time > time_limit:
#             print("Time limit reached.")
#             break
#         if fitness_limit is not None and best_fitness <= fitness_limit:
#             print("Fitness limit reached.")
#             break
#         if stall_generations is not None and prev_best_fitness is not None:
#             if best_fitness < prev_best_fitness:
#                 stall_count = 0
#             else:
#                 stall_count += 1
#             if stall_count == stall_generations:
#                 print("Stall generations limit reached.")
#                 break
#         if stall_time_limit is not None and prev_best_fitness is not None:
#             if time.time() - start_time - stall_time_limit >= 0:
#                 print("Stall time limit reached.")
#                 break
#
#         # 选择操作
#         selection_probabilities = 1 / (fitness_values - np.min(fitness_values) + 1e-10)
#         selection_probabilities /= np.sum(selection_probabilities)
#         selected_indices = np.random.choice(np.arange(len(population)), size=population_size, replace=True, p=selection_probabilities)
#         selected_population = population[selected_indices]
#
#         # 交叉操作
#         crossover_indices = np.random.choice(population_size, size=population_size // 2, replace=False)
#         crossover_pairs = selected_population[crossover_indices]
#         crossover_points = np.random.rand(population_size // 2, 1)
#         crossover_offspring = np.zeros_like(crossover_pairs)
#         for i in range(crossover_pairs.shape[0]):
#             crossover_offspring[i] = crossover_pairs[i, 0] * (1 - crossover_points[i]) + crossover_pairs[i, 1] * crossover_points[i]
#
#         # 变异操作
#         mutation_mask = np.random.rand(population_size // 2, 2) < mutation_rate
#         mutation_offspring = crossover_offspring + mutation_mask * np.random.uniform(low=-0.5, high=0.5, size=(population_size // 2, 2))
#
#         # 合并新一代种群
#         population = np.concatenate([crossover_offspring, mutation_offspring], axis=0)
#
#         # 更新变量
#         prev_best_fitness = best_fitness
#
#     return best_fitness_history, best_individual_history
#
# # 设定参数
# population_size = 100
# generations = 100
# crossover_rate = 0.8
# mutation_rate = 0.1
# search_range = [-5.12, 5.12]
# time_limit = 60  # 运行时间限制为 60 秒
# fitness_limit = -80.71  # 适应度值达到 -80.71 时终止算法
# stall_generations = 10  # 连续 10 次没有更新最优解时终止算法
# stall_time_limit = 10  # 如果连续 10 秒没有更新最优解则终止算法
#
# # 运行遗传算法
# best_fitness_history, best_individual_history = genetic_algorithm(population_size, generations, crossover_rate, mutation_rate, search_range, time_limit, fitness_limit, stall_generations, stall_time_limit)
#
# # 打印最终结果
# print("Best fitness:", best_fitness_history[-1])
# print("Best individual:", best_individual_history[-1])
#
# # 绘制最佳适应度图
# plt.figure(figsize=(8, 6))
# plt.plot(best_fitness_history, label='Best Fitness')
# plt.xlabel('Generation')
# plt.ylabel('Fitness')
# plt.title('Convergence of Genetic Algorithm')
# plt.legend()
# plt.grid(True)
# plt.show()

